package DateTimeFormatter;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * 使用 LocalDate、LocalTime、LocalDateTime
 */
public class TestLocalDate {
    private DayOfWeek dayOfWeek;

    @Test
    public void testLocalDate() {
        LocalDate ldt = LocalDate.now();
        System.out.println(ldt);
    }

    @Test
    public void testLocalTime() {
        LocalTime ldt = LocalTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalTime.of(12, 04, 33, 465));
    }

    @Test
    public void testLocalDateTime() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33));
    }

    /**
     * plusDays, plusWeeks,
     * plusMonths, plusYears
     */
    @Test
    public void testPlusDays() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).plusDays(2));
    }

    @Test
    public void testPlusWeeks() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).plusWeeks(2));
    }

    @Test
    public void testPlusMonths() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).plusMonths(2));
    }

    @Test
    public void testPlusYears() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).plusYears(2));
    }

    /**
     * minusDays, minusWeeks,
     * minusMonths, minusYears
     */
    @Test
    public void testMinusDays() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).minusDays(2));
    }

    @Test
    public void testMinusWeeks() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).minusWeeks(2));
    }

    @Test
    public void testMinusMonths() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).minusMonths(2));
    }

    @Test
    public void testMinusYears() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println("-----------------------");
        System.out.println(LocalDateTime.of(2021, 10, 19, 11, 04, 33).minusYears(2));
    }

    /**
     * Instant 时间戳
     */
    @Test
    public void testInstant() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime);
    }

    /**
     * getDayOfMonth 获得月份天数(1-31)
     * getDayOfYear 获得年份天数(1-366)
     * getDayOfWeek 获得星期几(返回一个 DayOfWeek 枚举值)
     * getMonth 获得月份, 返回一个 Month 枚举值
     * getMonthValue 获得月份(1-12)
     * getYear 获得年份
     */
    @Test
    public void testGetDayOfMonth() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getDayOfMonth());
    }

    @Test
    public void testGetDayOfYear() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getDayOfYear());
    }

    @Test
    public void testGetDayOfWeek() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getDayOfWeek());
    }

    @Test
    public void testGetMonth() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getMonth());
    }

    @Test
    public void testGetMonthValue() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getMonthValue());
    }

    @Test
    public void testGetYear() {
        Instant ins = Instant.now();
        System.out.println(ins);
        OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));//偏移量
        System.out.println(offsetDateTime.getYear());
    }


    /**
     * Duration 和 Period
     */
    @Test
    public void testDuration() {
        Instant ins = Instant.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        Instant ins1 = Instant.now();
        Duration duration = Duration.between(ins, ins1);
        System.out.println(duration.toMillis());
    }

    @Test
    public void testPeriod() {
        LocalDate ldt = LocalDate.now();
        LocalDate localDateTime = LocalDate.of(2021, 10, 19).plusYears(2);
        Period between = Period.between(localDateTime, ldt);
        System.out.println(between);
    }

    /**
     * TemporalAdjuster : 时间校正器
     */
    @Test
    public void testTemporalAdjuster() {
        LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime localDateTime = ldt.withDayOfMonth(10);
        LocalDateTime with = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));

        //自定义
        LocalDateTime ldt2 = ldt.with((x) -> {
            LocalDateTime ldt1 = (LocalDateTime) x;
            DayOfWeek dayOfWeek = ldt1.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt1.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt1.plusDays(2);
            } else {
                return ldt1.plusDays(1);
            }
        });
        System.out.println(ldt2);
    }

    /**
     * DateTimeFormatter
     */
    @Test
    public void testDateTimeFormatter() {
        DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String format = ldt.format(isoDate);
        System.out.println(format);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss");
        String format1 = ldt.format(dateTimeFormatter);
        System.out.println(format1);
    }

    /**
     * ZoneIdDate ZonedTime ZonedDateTime
     */
    @Test
    public void testZonedDate() {
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        zoneIds.forEach(System.out::println);
    }
}